This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
knitr::kable(iris[1:5, ], caption = 'A caption')
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
yield <- 200 * runif(100)
mean_yield <- mean(yield)
The average yield is 88.5450587.
See the source rmd file:
code (do not put r right after the first tick mark)echo and evalR code and results
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
R code suppressed
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
Only R code (R code was not evaluated)
summary(cars)
Both R codes and results not shown
messages and warningslibrary(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.1 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
detach("package:tidyverse", unload=TRUE)
library(tidyverse)
detach("package:tidyverse", unload=TRUE)
library(tidyverse)
detach("package:tidyverse", unload=TRUE)
resultsNo results are shown.
a <- runif(100)
But, this shows the above code was evaluated and object a was created.
a
## [1] 0.34441401 0.76159855 0.83399121 0.32353801 0.72001626 0.85604433
## [7] 0.94021265 0.20348534 0.96431681 0.23041607 0.10492717 0.52950996
## [13] 0.14212269 0.01751952 0.40722341 0.39301271 0.25584465 0.83378234
## [19] 0.57006298 0.38410787 0.77259035 0.15017864 0.43469966 0.80583628
## [25] 0.99929015 0.08105808 0.64105981 0.34568689 0.88694178 0.40511914
## [31] 0.92674366 0.20299659 0.87772978 0.94530068 0.56868103 0.70460739
## [37] 0.79688186 0.71190873 0.09818603 0.46052652 0.79429821 0.08940891
## [43] 0.65283050 0.54971379 0.14717854 0.50782563 0.23783175 0.01201778
## [49] 0.91926172 0.08599764 0.27859885 0.80251517 0.16938050 0.70942840
## [55] 0.64135258 0.23865756 0.10734355 0.69965658 0.59806245 0.98987975
## [61] 0.68860552 0.70374886 0.16050336 0.86414268 0.50133718 0.93499768
## [67] 0.73695654 0.62993393 0.59452912 0.33710306 0.71635117 0.43964090
## [73] 0.30036933 0.13774093 0.81765636 0.67183704 0.41804543 0.68170864
## [79] 0.02826463 0.93650054 0.72174247 0.37771118 0.09576138 0.55115967
## [85] 0.90233756 0.60520135 0.11726092 0.08661284 0.61003548 0.93806585
## [91] 0.52176870 0.74090642 0.65246894 0.53837009 0.48910940 0.62029891
## [97] 0.33685691 0.51933751 0.96738346 0.10552652
include = FALSENo results are shown.
But, this shows the above code was evaluated and object a was created.
a
## [1] 0.02435988 0.40211995 0.34232859 0.30377867 0.76112668 0.96383618
## [7] 0.42202507 0.63757880 0.94618432 0.80397407 0.44757417 0.28547742
## [13] 0.97242358 0.93658663 0.23011669 0.60959234 0.86266106 0.06661361
## [19] 0.09996879 0.89552850 0.47270991 0.68359813 0.27902049 0.68914464
## [25] 0.30067027 0.64856617 0.91161594 0.03551053 0.44511407 0.31872800
## [31] 0.02966430 0.49407022 0.11887189 0.34456793 0.43044078 0.36718360
## [37] 0.47107678 0.21903759 0.80279600 0.84904276 0.08043380 0.44884781
## [43] 0.47320823 0.83674139 0.23311225 0.16045845 0.48872826 0.11963946
## [49] 0.01188965 0.86061122 0.07244497 0.02186055 0.71175426 0.91085773
## [55] 0.02937777 0.64959782 0.16868192 0.66793593 0.09526204 0.11675944
## [61] 0.75642980 0.86143044 0.41932796 0.75584318 0.81429017 0.86131824
## [67] 0.58846486 0.74167820 0.22258814 0.17551208 0.07365811 0.85494693
## [73] 0.10911197 0.14939258 0.96167427 0.91777743 0.18566852 0.46982266
## [79] 0.41837695 0.85883350 0.04429182 0.43413501 0.18708909 0.27303081
## [85] 0.49673153 0.75663537 0.07793540 0.43111106 0.15927581 0.56035464
## [91] 0.64744472 0.72649248 0.72134870 0.25506268 0.30749171 0.72147846
## [97] 0.57317993 0.24668480 0.41161429 0.80122345
#--- repeat steps 1-3 B times ---#
MC_sim <- function(i){
N <- 50000 # sample size
#--- steps 1 and 2: ---#
mu <- rnorm(N) # the common term shared by both x and u
x <- rnorm(N) + mu # independent variable
v <- rnorm(N) + mu # error
y <- 1 + x + v # dependent variable
data <- data.frame(y = y, x = x)
#--- OLS ---#
reg <- lm(y~x, data = data) # OLS
#--- return the coef ---#
return(reg$coef['x'])
}
MC_results <- lapply(1:1000, MC_sim) %>%
unlist() %>%
mean()
MC_results
## [1] 1.499967
MC_results
## [1] 1.499967
library(DT)
iris %>% datatable(
extensions = 'Buttons',
options = list(
dom = 'Blfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
lengthMenu = list(c(10,25,50,-1),
c(10,25,50,"All"))
)
)
library(dygraphs)
dygraph(nhtemp, main = "New Haven Temperatures") %>%
dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
p <- ggplot(data = diamonds) +
geom_bar(aes(x = cut, fill = clarity), position = "dodge")
ggplotly(p)